package com.cn.ccp.action.onlineEdu;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseBean;
import com.cn.ccp.bean.CourseSettingBean;
import com.cn.ccp.bean.OnlineExamBean;
import com.cn.ccp.bean.SelectedCourseBean;
import com.cn.ccp.bean.UserBean;
import com.cn.ccp.util.CCPContants;

public class OnlineExamAction extends BaseAction {
	private Logger log = Logger.getLogger(OnlineExamAction.class);
	private String courseName ;
	private String total_score ;//试题总分
//	private String total_time ;//总时间
	
	private String is_chapter ;//是否为章节ID， 0：不是  ； 1：是
	private String selectedCourseId;//学生选课id
	
	private boolean showSubmitButton = true; //是否显示交卷 和 临时保存按钮
	private  List<OnlineExamBean> onlineExamList ;
	private String settingId;//课程设置id
	private String strStudentId ;
	
	
	private boolean showHasMarked = false ;// xians
	
	public String tempSave(){
		//检查是否已经做完题目
		boolean flag = getOnlineExamService().saveSelectValueChange(onlineExamList);
		FacesContext context=FacesContext.getCurrentInstance();
		if(flag)
			context.addMessage("success", new FacesMessage(getMessages("onlien_test_page_temp_save_success")));
		else
			context.addMessage("error", new FacesMessage(getMessages("onlien_test_page_temp_save_error")));
		return MAIN ;
	}
	public String showTest(){
		log.info("-----------goToTest()--------------");
		return UPDATE;
	}
	
	
	
	public void charptsave(){
		for (int i = 0; i < onlineExamList.size(); i++) {
			OnlineExamBean onb = onlineExamList.get(i);
			if(onb.getSelectAnswer() != null && !onb.getSelectAnswer().equals("")){
				onb.setShowAnswerInfo(true);
				onlineExamList.set(i, onb);
			}
		}
	}
	
	
	public String backFromContents(){
		return "main";
	}

	
	public String save(){
		//检查是否已经做完题目
		int tatol_score = 0 ;
		int reality_score = 0 ;//实际分数
		//已选课程状态： 
		//0--等等审批（对于需要收费的课程来说，在学生选择了该科后状态为0,等等学校领导审批，审批通过后状态进入1，开始学习） 
		//1--正在学习
		//2--已经结业 
		//3--考试未通过
		//4--审批未通过 
		//-1---逻辑删除状态
		int status = 0 ;
		FacesContext context=FacesContext.getCurrentInstance();
		for (int i = 0; i < onlineExamList.size(); i++) {
			OnlineExamBean bean = onlineExamList.get(i);
			if(bean.getSelectAnswer() == null || bean.getSelectAnswer().equals("")){
				context.addMessage("error", new FacesMessage(getMessages("onlien_test_page_save_error_1")+":"+bean.getIndex()));
				return "";
			}else{
				tatol_score = tatol_score + bean.getScore() ;
				if(bean.getSelectAnswer().equalsIgnoreCase(bean.getAnswer()))
					reality_score = reality_score + bean.getScore();
			}
		}
		double hegeFen = tatol_score*0.6 ;
		
		if(reality_score < hegeFen){
			status = 3 ;
		}else{
			status = 2 ;
		}
		
//		log.info("----------settingId = "+settingId);
//		boolean flag = getOnlineExamService().updateSelectCourseInfo(selectedCourseId, ""+reality_score, ""+status);
//		if(flag){
//			context.addMessage("success", new FacesMessage(getMessages("onlien_test_page_save_success")));
//			getCourseInvestigationService().updateCourseSetRecord(settingId,""+tatol_score);
//		}else{
//			context.addMessage("error", new FacesMessage(getMessages("onlien_test_page_save_error")));
//		}
		
		log.info("-----settingId = "+settingId);
		getCourseInvestigationService().commintCourseSetting(settingId, selectedCourseId,  ""+reality_score, ""+status, onlineExamList);
		
		return MAIN ;
	}
	
	public String goDetailPage(){
		return MAIN;
	}
	
	public String goDoSettingPages(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		settingId = varMap.get("settingId");
		String settingType = varMap.get("settingType");
		selectedCourseId = varMap.get("selectedCourseId");
		log.info("-----------settingId = "+settingId);
		log.info("-----------settingType = "+settingType);
		log.info("-----------selectedCourseId = "+selectedCourseId);
		int type = Integer.parseInt(settingType.trim());
		String typeCourse = "examination";
		SelectedCourseBean b = (SelectedCourseBean) getSelectCourseService().getSelectedCourseBySelectedId(selectedCourseId);
		
		showHasMarked = b.isHasMarked();
		if(type == 1){//考试
			CourseSettingBean csb = getCourseService().getCourseSetingBean(settingId);
			this.total_score = csb.getScore() ;
			typeCourse =  "examination";
			onlineExamList = getOnlineExamService().getCourseResearch(settingId);
		}else if(type == 2){//作业
			typeCourse =  "training";
			onlineExamList = getOnlineExamService().getCourseResearch(settingId);
		}else if(type == 3){//调查
			onlineExamList = getOnlineExamService().getCourseResearch(settingId);
			typeCourse =  "evaluation";
		}
		return typeCourse;
	}
	
	/**
	 * 提交调查
	 */
	public String commintResearch(){
		getCourseInvestigationService().commintInvestigation(settingId, onlineExamList);
		return MAIN;
	}
	
	/**
	 * 提交作业
	 * @return
	 */
	public void commintTraining(){
		getCourseInvestigationService().commintTraining(settingId);
	}
	
	
	
	/**
	 * 进入考试
	 * @return
	 */
	public String goToTest(){
		showSubmitButton = true ;
		log.info("-----------goToTest()--------------");
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String courseid = varMap.get("courseid");
		String chapterid = varMap.get("chapter");
		if(varMap.get("selectedCourseId") != null)
			selectedCourseId = varMap.get("selectedCourseId");
		log.info("----------courseid = "+courseid);
		log.info("----------chapterid = "+chapterid);
		log.info("----------selectedCourseId = "+selectedCourseId);
		if(courseid != null && !courseid.equals("") && !courseid.equals("null")){
			is_chapter = "0" ;
			CourseBean bean = getCourseService().getBeanById(courseid);
			courseName = bean.getCourse_name();
			total_score = bean.getTotal_score() ;
			onlineExamList = getOnlineExamService().getOnlineExamListByCourseId(courseid,selectedCourseId);
		}else if(chapterid != null && !chapterid.equals("") && !chapterid.equals("null")){
			is_chapter = "1" ;
			CourseBean bean = getCourseService().getBeanByCapterId(chapterid);
			courseName = bean.getCourse_name();
			total_score = bean.getTotal_score() ;
			onlineExamList = getOnlineExamService().getOnlineExamListByChapterTd(chapterid);
		}
		return UPDATE;
	}
		
	public String goToShowTest(){
		showSubmitButton = false ;
		log.info("-----------goToTest()--------------");
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String courseid = varMap.get("courseid");
		String chapterid = varMap.get("chapter");
		if(varMap.get("selectedCourseId") != null)
			selectedCourseId = varMap.get("selectedCourseId");
		log.info("----------courseid = "+courseid);
		log.info("----------chapterid = "+chapterid);
		log.info("----------selectedCourseId = "+selectedCourseId);
		if(courseid != null && !courseid.equals("") && !courseid.equals("null")){
			is_chapter = "0" ;
			CourseBean bean = getCourseService().getBeanById(courseid);
			courseName = bean.getCourse_name();
			total_score = bean.getTotal_score() ;
			onlineExamList = getOnlineExamService().getOnlineExamListByCourseId(courseid,selectedCourseId);
		}else if(chapterid != null && !chapterid.equals("") && !chapterid.equals("null")){
			is_chapter = "1" ;
			CourseBean bean = getCourseService().getBeanByCapterId(chapterid);
			courseName = bean.getCourse_name();
			total_score = bean.getTotal_score() ;
			onlineExamList = getOnlineExamService().getOnlineExamListByChapterTd(chapterid);
		}
		if(onlineExamList != null && onlineExamList.size() > 0 ){
			for (int i = 0; i < onlineExamList.size(); i++) {
				OnlineExamBean bean = onlineExamList.get(i);
				bean.setSelectAnswer(bean.getAnswer());
				bean.setShowAnswerInfo(true);
				onlineExamList.set(i, bean);
			}
		}
		return UPDATE;
	}
	
	public String goToMark(){
		log.info("-----------goToMark()--------------");
		return "marked";
	}
	
	
	
	public String getCourseName() {
		return courseName;
	}
	public void setCourseName(String courseName) {
		this.courseName = courseName;
	}
	public String getTotal_score() {
		return total_score;
	}
	public void setTotal_score(String total_score) {
		this.total_score = total_score;
	}

	public List<OnlineExamBean> getOnlineExamList() {
		UserBean userBean = CCPContants.getUserBean();
		strStudentId = String.valueOf(userBean.getId());
	
		if(onlineExamList == null)
			onlineExamList = new ArrayList<OnlineExamBean>();
		return onlineExamList;
	}

	
	public void setOnlineExamList(List<OnlineExamBean> onlineExamList) {
		this.onlineExamList = onlineExamList;
	}

 
	public String getIs_chapter() {
		return is_chapter;
	}


	public void setIs_chapter(String is_chapter) {
		this.is_chapter = is_chapter;
	}


	public String getSelectedCourseId() {
		return selectedCourseId;
	}


	public void setSelectedCourseId(String selectedCourseId) {
		this.selectedCourseId = selectedCourseId;
	}
	public boolean isShowSubmitButton() {
		return showSubmitButton;
	}
	public void setShowSubmitButton(boolean showSubmitButton) {
		this.showSubmitButton = showSubmitButton;
	}
	public String getSettingId() {
		return settingId;
	}
	public void setSettingId(String settingId) {
		this.settingId = settingId;
	}
	public boolean isShowHasMarked() {
		return showHasMarked;
	}
	public void setShowHasMarked(boolean showHasMarked) {
		this.showHasMarked = showHasMarked;
	}
	public String getStrStudentId() {
		return strStudentId;
	}
	public void setStrStudentId(String strStudentId) {
		this.strStudentId = strStudentId;
	}
	
	
	
}
